<?php

	/**
	 * Beer Item
	 *
	 * @author anton.perkin
	 * @version 1.0
	 *
	 */
	class BeerItem
	{
		/* VARIABLES */
		
		public $id;
		public $name;
		public $brewery;
		public $category;
		public $style;
		public $available;
		public $abv;
		public $ibu;
		public $color;
		public $description;
		public $webAddress;
		public $photoURL;
		public $source;
		public $urlId;
		
		
		/* METHODS */
		
		/**
		 * Saves BeerItem into database
		 *
		 */
		public function Save()
		{
			if (DB::ExecuteSQL("
				INSERT INTO t_beers
				(
					beer_id,
					name,
					brewery,
					category,
					style,
					available,
					abv,
					ibu,
					color,
					description,
					photo_url,
					web_address,
					source,
					url_id
				)
				VALUES
				(
					$this->id,
					'".addslashes($this->name)."',
					'".addslashes($this->brewery)."',
					'".addslashes($this->category)."',
					'".addslashes($this->style)."',
					'".addslashes($this->available)."',
					'".addslashes($this->abv)."',
					'".addslashes($this->ibu)."',
					'".addslashes($this->color)."',
					'".addslashes($this->description)."',
					'$this->photoURL',
					'$this->webAddress',
					'$this->source',
					$this->urlId
				);
			") === true)
			{
				$this->Log(true);
				$result = true;
			}	
			else 
			{
				$this->Log(false);
				$result = false;
			}
			
			return $result;
		}
		
		/**
		 * Saves beer URL into database
		 *
		 * @param string $url url
		 * @param string $source source
		 * 
		 * @return boolean
		 * 
		 */
		public function SaveURL($url, $source)
		{
			if (DB::ExecuteSQL("
				INSERT INTO t_beer_urls
				(
					url,
					source
				)
				VALUES
				(
					'$url',
					'$source'
				);
					
			") === true)
			{
				$this->LogURL($url, $source, true);
				$result = true;
			}	
			else 
			{
				$this->LogURL($url, $source, false);
				$result = false;
			}	

			return $result;
		}
		
		/**
		 * Gets beer list from DB
		 *
		 * @param int $offset
		 * @param int $number
		 */
		public function GetList($offset, $number)
		{
			//limit
			if (($offset == '') && ($number == ''))
			{
				$limit = '';
			}
			else 
			{
				$offset = ($offset == '') ? 0 : $offset;
				$number = ($number == '') ? 1000000 : $number;
				$limit = "LIMIT $offset, $number";
			}
			
			$beers = DB::ListItems("
				SELECT * FROM t_beers $limit;
			");	

			return $beers;
		}
		
		/**
		 * Gets and saves beer image
		 *
		 * @param array $beerItem
		 */
		public function CopyImage($beerItem)
		{
			$beerDB = new BeerDB();
			$image = $beerDB->GetBeerItemImage($beerItem['beer_id']);
			
			if (!empty($image))
			{
				$this->id = $beerItem['beer_id'];
				if (DB::ExecuteSQL("
					UPDATE t_beers
					SET
						photo_url = '$image'
					WHERE
						beer_id = {$beerItem['beer_id']};
				") === true)
				{
					$imageInfo = pathinfo($image);
					$copyResult = copy($image, $_SERVER['DOCUMENT_ROOT'].'/beers/results/images/'.$beerItem['id'].'.'.$imageInfo['extension']);
					$this->Log($copyResult);
					$result = $copyResult;
				}	
				else 
				{
					$this->Log(false);
					$result = false;
				}
			}
			else 
			{
				$result = true;
			}
			
			return $result;
		}
		
		/**
		 * Gets and saves brewery country
		 *
		 * @param array $beerItem
		 */
		public function CrawlCountry($beerItem)
		{
			$beerDB = new BeerDB();
			$country = $beerDB->GetBeerItemCountry($beerItem['beer_id']);
			
			if (!empty($country))
			{
				$this->id = $beerItem['beer_id'];
				if (DB::ExecuteSQL("
					UPDATE t_beers
					SET
						country = '$country'
					WHERE
						beer_id = {$beerItem['beer_id']};
				") === true)
				{
					$this->Log(true);
					$result = true;
				}	
				else 
				{
					$this->Log(false);
					$result = false;
				}
			}
			else 
			{
				$result = true;
			}
			
			return $result;
		}		
		
		/**
		 * Creates log entry for the inserted beer
		 * 
		 * @param boolean $success result
		 *
		 */
		private function Log($success)
		{
			$color = ($success) ? 'green' : 'red';

			echo "<span style = 'color: $color; '>Id: $this->id</span><br />";			
		}

		/**
		 * Creates log entry for the inserted beer URL
		 *
		 * @param string $url url
		 * @param string $source source
		 * @param boolean $success result
		 */
		private function LogURL($url, $source, $success)
		{
			$color = ($success) ? 'green' : 'red';
			
			echo "<span style = 'color: $color; '>
			URL: $url | Source: $source
			</span><br />";
		}
		
	}

?>